package com.xiaoyg.receive;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author xiaoyg
 * @Title: ReceiveLogsToConsole
 * @ProjectName rabbitmq-subscribe
 * @Description: TODO
 * @date 2018/12/413:34
 */
public class ReceiveLogsToConsole {

    //转发器
    private final static String EXCHANGE_NAME = "exchange_log";

    public static void main(String[] args) throws IOException, InterruptedException {
        //创建连接和频道
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.8.129");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123456");
        connectionFactory.setPort(AMQP.PROTOCOL.PORT);

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");

        String queueName = channel.queueDeclare().getQueue();

        channel.queueBind(queueName,EXCHANGE_NAME,"");

        System.out.println("* 等待消息，按Ctrl + C 结束！");

        QueueingConsumer consumer = new QueueingConsumer(channel);

        channel.basicConsume(queueName,true,consumer);

        while (true){
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println("[x] Received '"+message+"'");
        }

    }


}
